<div ng-if="pagedata.op=='print' || pagedata.op=='setfa'" class="col-jy" style="width:100%;">
<div class="panel form-horizontal">
<div class="page-header">
<h4 class="ng-binding">打印快递单</h4>
</div>
<div class="panel-body">
<div ng-if="!pagedata.item.fahuo_logid" class="form-group ng-scope">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label ng-binding" ng-class="{'must':false}">快递公司</label>
	<div class="col-xs-12 col-sm-8 col-lg-9">
		<select class="form-control" ng-model="pagedata.item.kuaikey">
			<option value="" selected="selected">请选择</option>
			<option ng-repeat="op in pagedata.express" ng-selected="pagedata.item.kuaikey==op.id" value="{{op.id}}">顺丰速运</option>
		</select>
		<div class="help-block"></div>
	</div>
</div>
<div ng-if="!pagedata.item.fahuo_logid" class="form-group">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label">是否预约上门</label>
	<div class="col-xs-12 col-sm-8 col-lg-9">
		<label ng-if="'{isradio}'!=1" class="right-label ng-scope">
			<div ng-show="pagedata.item.isyue==1" class="switch switchOn ng-hide" ng-click="pagedata.item.isyue=0"></div>
			<div ng-show="pagedata.item.isyue!=1" class="switch" ng-click="pagedata.item.isyue=1"></div>
		</label>
		<div class="help-block"></div>
	</div>
</div>
<div ng-if="!pagedata.item.fahuo_logid" class="form-group ng-scope ng-hide" ng-show="check_show(pagedata.item,{'isyue':1})">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label ng-binding" ng-class="{'must':false}">预约上门收件时间</label>
	<div class="col-sm-4 col-xs-6"><input type="text" name="starttime" onchange="window.scope.pagedata.item.starttime=$(this).val()" ng-model="pagedata.item.starttime" placeholder="请选择预约上门收件时间" readonly="false" class="datetimepicker form-control ng-pristine ng-untouched ng-valid ng-empty" style="padding-left:12px;"></div>
	<div class="help-block"></div>
</div>
<script type="text/javascript">
					require(["datetimepicker"], function(){
						$(function(){
								var fmt;
								if('default'=='day'){
									fmt="Y-m-d";
								}else if('default'=='year'){
									fmt="Y";
								}else if('default'=='month'){
									fmt="Y-m";
								}else{
									fmt="Y-m-d H:i";
								}
								var option = {
									lang : "zh",
									step : 5,
									timepicker : true,
									closeOnDateSelect : true,
									format : fmt,
									
								};
							$(".datetimepicker[name = 'starttime']").datetimepicker(option);
							$(".datetimepicker[name = 'endtime']").datetimepicker(option);
						});
});
</script>
<div ng-if="!pagedata.item.fahuo_logid" class="form-group ng-scope ng-hide" ng-show="check_show(pagedata.item,{'isyue':1})">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label ng-binding" ng-class="{'must':false}">预约上门截止时间</label>
	<div class="col-sm-4 col-xs-6"><input type="text" name="endtime" onchange="window.scope.pagedata.item.endtime=$(this).val()" ng-model="pagedata.item.endtime" placeholder="请选择预约上门截止时间" readonly="false" class="datetimepicker form-control ng-pristine ng-untouched ng-valid ng-empty" style="padding-left:12px;"></div>
	<div class="help-block"></div>
</div>
<div class="form-group">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label ng-binding">打印机服务器:</label>
	<div class="col-xs-12 col-sm-8 col-lg-9">
		<div class="input-group">
			<input type="text" value="ws://localhost:13528" class="form-control" autocomplete="off" readonly="readonly">
			<span class="input-group-btn">
				<button class="btn btn-gray" type="button" ng-click="clip('ws://localhost:13528')">{{check_show_lang('复制','copy')}}</button>
			</span>
		</div>
		<div class="help-block">固定</div>
	</div>
</div>
<div class="form-group">
	<label class="col-xs-12 col-sm-3 col-md-2 right-label ng-binding">链接状态</label>
	<div class="col-xs-12 col-sm-8 col-lg-9">
		<div class="input-group">
			<input type="text" value="{{pagedata.connectionStatus}}" class="form-control" autocomplete="off" readonly="readonly">
			<span class="input-group-btn">
				<button class="btn btn-gray" type="button" ng-click="pagedata.doConnect()">刷新</button>
			</span>
		</div>
		<div class="help-block">如果链接异常，请检查是否已安装菜鸟打印组件：https://page.cainiao.com/waybill/cloud_printing/home.html</div>
	</div>
</div>
</div>
</div>
<div class="modal-sub">
<button class="btn btn-primary" ng-click="pagedata.dopost(1)">获取面单并预览</button>
<button class="btn btn-red" ng-click="pagedata.dopost()">获取面单并打印</button>
</div>
<div id="showpdf" class="modal fade" tabindex="-1" style="z-index: 99999; display: none;">
	<div class="modal-dialog" style="width:920px;max-height:480px">
	<div class="modal-content" style="border-radius: 4px;">
	    <div class="modal-header"><h3>预览面单</h3></div>
        <div class="modal-body form-horizontal tab-content">
        	<div class="c">
        		<img ng-src="{{tomedia(pagedata.pdf[0])||'../core/resource/images/nopic.jpg'}}" style="width:100%"/>
        	</div>
        </div>
        <div class="modal-footer">
        <a class="btn btn-primary" data-dismiss="modal" aria-hidden="true" ng-click="pagedata.dopost()">打印</a>
        <a class="btn btn-warning" data-dismiss="modal" aria-hidden="true">取消</a>
        </div>
	</div>
	</div>
</div>
<script type="text/javascript">
scope.pagedata.socket='';
scope.pagedata.doConnect=function(callback){
	var serviceUrl = 'ws://localhost:13528';
	scope.pagedata.socket = new WebSocket(serviceUrl);
	// 监听消息
	scope.pagedata.socket.onmessage = function(event){ 
		console.log('Client received a message',event); 
		// parse json
		//console.log('event.data',event.data); 
		var obj = eval('('+event.data+')');
		//console.log('obj',obj); 
		if (obj && (obj.previewURL || obj.previewImage)){
			var url;
			if (obj && obj.previewURL) {
				url = obj.previewURL;
			} else if (obj && obj.previewImage) {
				url = obj.previewImage;
			}
			console.log('pdf',url); 
			scope.pagedata.pdf=url
		}
		if(scope.pagedata.isPreview){
			$('#showpdf').modal();
			scope.$evalAsync();
		}else{
		    scope.message("操作成功",'','/order/');
		    return '';
		}
	};
	scope.pagedata.socket.onopen = function(event){
		if (callback != null) {
			callback();
		}
		scope.pagedata.connectionStatus="已连接";
		scope.$evalAsync();
	}
	scope.pagedata.socket.onerror = function(error)
	{
		console.log("Failed to connect CN print at " + serviceUrl, error);
		scope.pagedata.connectionStatus="连接错误";
		scope.$evalAsync();
	}
	// 监听Socket的关闭
	scope.pagedata.socket.onclose = function(event)
	{ 
		console.log('Client notified socket has closed',event);
		scope.pagedata.connectionStatus="连接关闭";
		scope.$evalAsync();
	}; 
}
scope.pagedata.doConnect();
scope.pagedata.sendCommand=function(res){
	if (typeof scope.pagedata.socket == "undefined" || scope.pagedata.socket.readyState == WebSocket.CLOSED) {
		scope.pagedata.doConnect(sendCommand);
		return;
	}
	if (scope.pagedata.socket.readyState != WebSocket.OPEN) {
		scope.message("无效连接: " + scope.pagedata.socket.readyState);
		return;
	}
	var msg = res.printmessage;
	var repeatCount = 1;
	var isPreview = scope.pagedata.isPreview;
	var time = new Date().getTime();
	for (var index = 0; index < repeatCount; index++) {
		var obj = msg;
		if (obj["task"]){
			// taskID
			obj["task"]["taskID"] = time.toString() + "_TASK_" + index;
			// preview
			obj["task"]["preview"] = isPreview;
		}	
		var newMsg = JSON.stringify(obj);
		//console.log('newMsg');console.log(newMsg);
		scope.pagedata.socket.send(newMsg);
	}
}
scope.pagedata.dopost=function(isPreview){
	scope.pagedata.isPreview=isPreview?true:false
	scope.pagedata.item.preview=scope.pagedata.isPreview?1:0;
	scope.post_json(scope.pagedata.op,scope.pagedata.item,function(res){
		console.log(res);
		scope.pagedata.sendCommand(res);
		scope.$evalAsync();
	});
}
</script>
</div>